const express = require('express');

const db = require('../db')

const router =express.Router();


//  ---------------------------------------------------获取社区消息数量------------------------------------------------------
router.get('/get_comment_count',(req,res)=>{
    const {user_id}=req.user
    db(`select count(1) as count from comment where user_id = ${user_id}`,(err1,res1)=>{
        if (err1) {
            res.send({
                code:-1,
                msg:err1.sqlMessage,
            })
            throw err1;
        }
        db(`select count(1) as count from comment`,(err2,res2)=>{
            if (err2) {
                res.send({
                    code:-1,
                    msg:err2.sqlMessage,
                })
                throw err2;
            }
            res.send({
                code:1,
                data:{
                    my_comment_count:res1[0].count,
                    comment_count:res2[0].count
                }
            })
        })
    })
})


// -----------------------------------------------------获取社区消息--------------------------------------------------------

router.get('/get_comment',(req,res)=>{
    const {is_me,limit,page}=req.query;
    const {user_id}=req.user
    let sql;
    // 是否获取我的
    if (is_me==0) {
        sql=`select *,(select count(1) from comment_support where comment.comment_id=comment_support.comment_id) as support_count,(select count(1) from comment_support where comment.comment_id=comment_support.comment_id && user_id = ${user_id}) as is_support from comment left join user on comment.user_id=user.user_id order by create_time desc` 
    }else{
        sql=`select *,(select count(1) from comment_support where comment.comment_id=comment_support.comment_id) as support_count,(select count(1) from comment_support where comment.comment_id=comment_support.comment_id && user_id = ${user_id}) as is_support from comment  left join user on comment.user_id=user.user_id  where comment.user_id = '${user_id}'  order by create_time desc` 
    }
    if (limit && page) {
        let start=` limit ${(page-1)*limit},${limit}`
        sql +=start;
    }
    db(sql, (err,data)=>{
        if (err) {
            res.send({
                code:-1,
                msg:err.sqlMessage,
            })
            throw err;
        }
        data.forEach(item => {
            item.image_url=item.image_url.split(',')
            item.user={
                user_id:item.user_id,
                phone:item.phone,
                nickname:item.nickname,
                sex:item.sex,
                avatar:item.avatar,
                profile:item.profile,
                age:item.age
            }
            delete item.user_id
            delete item.phone
            delete item.nickname
            delete item.sex
            delete item.avatar
            delete item.profile
            delete item.age
            delete item.password
        });
            res.send({
                code:1,
                data
            })
        
    })
})

// -----------------------------------------------------发布社区消息--------------------------------------------------------

router.post('/set_comment',(req,res)=>{
    const {content,files}=req.body;
    const {user_id}=req.user;
    const now=Date.parse(new Date())/1000
    db(`insert into comment(user_id,content,image_url,create_time) values ('${user_id}','${content}','${files}',${now})`,(err,data)=>{
        if (err) {
            res.send({
                code:-1,
                msg:err.sqlMessage,
            })
            throw err;
        }
        res.send({
            code:1,
            msg:'添加成功！'
        })
    })
})

// -----------------------------------------------------社区消息点赞--------------------------------------------------------

router.post('/is_support',(req,res)=>{
    const {comment_id}=req.body;
    const {user_id}=req.user;
    const now=Date.parse(new Date())/1000
    db(`select * from comment_support where user_id=${user_id} && comment_id=${comment_id}`,(err,data)=>{
        // 判断是否含有此条点赞，有的话取消点赞，无的话点赞
        if(data.length){
            db(`delete from comment_support where support_id = ${data[0].support_id}`,err=>{
                if (err) {
                    res.send({
                        code:-1,
                        msg:err.sqlMessage,
                    })
                    throw err;
                }
                res.send({
                    code:1,
                    msg:'取消点赞！'
                })
            })
        }else{
            db(`insert into comment_support(user_id,comment_id,create_time) values ('${user_id}','${comment_id}',${now})`,(err)=>{
                if (err) {
                    res.send({
                        code:-1,
                        msg:err.sqlMessage,
                    })
                    throw err
                }
                res.send({
                    code:1,
                    msg:'点赞成功！'
                })
            })
        }

    })
    
    
})

// -----------------------------------------------------删除社区消息--------------------------------------------------------

router.post('/del_comment',(req,res)=>{
    const {ids}=req.body;
    db(`delete from comment where comment_id  in (${ids})`,(err,data)=>{
        if (err) {
            res.send({
                code:-1,
                msg:err.sqlMessage,
            })
            throw err ;
        }
        db(`delete from comment_support where comment_id in (${ids})`,(err,data)=>{
            if (err) {
                res.send({
                    code:-1,
                    msg:err.sqlMessage,
                })
                throw err ;
            }
            res.send({
              code:1,
              msh:'删除成功！'
            })
        })
    })
})





module.exports=router